package com.dt.dubai.mall.service;

import com.dt.dubai.core.comm.ConstantValue;
import com.dt.dubai.mall.entity.MallWebCategoryEntity;
import com.dt.dubai.mall.service.innate.MallWebCategorySerInnateAbsImp;
import com.dt.dubai.mall.service.ser.MallWebCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
public class MallWebCategoryServiceImp extends MallWebCategorySerInnateAbsImp implements MallWebCategoryService {

    /*innateServiceStart*/
    @Override
    protected void userSavEntity(MallWebCategoryEntity inEntity) {

    }

    @Override
    protected void userModEntity(MallWebCategoryEntity inEntity, MallWebCategoryEntity upEntity) {
        upEntity.setLevel(inEntity.getLevel());
        upEntity.setCommodityType(inEntity.getCommodityType()); // 类型
        upEntity.setParentId(inEntity.getParentId()); // 父级id
        upEntity.setCategoryName(inEntity.getCategoryName()); // 分类名称
        upEntity.setCategoryAliasName(inEntity.getCategoryAliasName()); // 分类别名称
        upEntity.setIcon(inEntity.getIcon()); // 图标
        upEntity.setCategoryShowType(inEntity.getCategoryShowType()); // 是否展示
        upEntity.setYesShow(inEntity.getYesShow()); // 是否显示

    }
    /*innateServiceEnd*/
    /*以上内容请误删除或是移动，系统升级使用，static系统升级覆盖用户修改，用户代码请写入下方*/

    //删除一级目录需要判断下面是否有二级目录
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean delByKey(List<Long> recordIds, final Long userId) {
        List<Long> listRecordIds=new ArrayList<>();
        if (null == recordIds) return ConstantValue.FALSE;
        for (Long recordId : recordIds) {
            List<MallWebCategoryEntity> list=mallWebCategoryMapper.selectOneByParent(recordId);
            if(list.size()<1){
                listRecordIds.add(recordId);
            }
        }
        Example example = new Example(MallWebCategoryEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn(ConstantValue.recordId, listRecordIds);
        int result = mallWebCategoryMapper.deleteByExample(example);
        if (result == ConstantValue.INT_0)
            throw new RuntimeException("E49");
        if (result != recordIds.size())
            throw new RuntimeException("E81");
        return ConstantValue.TRUE;
    }

}
